package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.FacilityData;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther: bin.liu
 * @Date: 2018/12/13 9:53
 * @Description: 设备资料dao
 */
@Repository("facilityDataDao")
public class FacilityDataDao implements CrudDao<FacilityData> {

    private final static Logger logger = LoggerFactory.getLogger(FacilityDataDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash){
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("fd.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String searchCode = queryHash.get("searchCode");
        if (!StringUtils.isEmpty(searchCode)) {
            conditionHash.put("(fd.facilityDataName like ?{paramIndex}) ", "%" + searchCode + "%");
        }
        String dataCatId = queryHash.get("dataCatId");
        if (!StringUtils.isEmpty(dataCatId)) {
            conditionHash.put(" exists( select dcs from fd.dataCatSet dcs where dcs.id = ?{paramIndex})", StringConverters.null2Long(dataCatId));
        }
        String dataCatName = queryHash.get("dataCatName");
        if (!StringUtils.isEmpty(dataCatName)) {
            conditionHash.put(" exists( select dcs from fd.dataCatSet dcs where dcs.dataCatName like ?{paramIndex})", "%" + dataCatName + "%");
        }
        String facilityId = queryHash.get("facilityId");
        if (!StringUtils.isEmpty(facilityId)) {
            conditionHash.put(" exists( select fs from fd.facilitySet fs where fs.id = ?{paramIndex})", StringConverters.null2Long(facilityId));
        }
        String facilityName = queryHash.get("facilityName");
        if (!StringUtils.isEmpty(facilityName)) {
            conditionHash.put(" exists( select fs from fd.facilitySet fs where fs.name like ?{paramIndex})", "%" + facilityName + "%");
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(fd.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String facilityDataName = queryHash.get("facilityDataName");
        if (!StringUtils.isEmpty(facilityDataName)) {
            conditionHash.put("(fd.facilityDataName = ?{paramIndex}) ", facilityDataName);
        }
        String serialNum = queryHash.get("serialNum");
        if (!StringUtils.isEmpty(serialNum)) {
            conditionHash.put("(fd.serialNum = ?{paramIndex}) ", serialNum);
        }

        return conditionHash;
    }

    /**
     * 获取总记录数
     */
    @Override
    public Long totalRecord(Map<String, String> queryHash){
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(fd) from FacilityData fd ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    /**
     * 分页列表
     */
    @Override
    public List<FacilityData> findList(QueryParam queryParam){
        String sqlInfo = queryParam.joinJPQL("select fd from FacilityData fd ", "order by fd.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<FacilityData> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, FacilityData.class);
        return queryParam.findPageList(typedQuery);
    }

    /**
     * id获取记录
     */
    @Override
    public FacilityData findModel(Long id){
        return entityManager.find(FacilityData.class, id);
    }

    /**
     * 增加记录
     */
    @Override
    public Integer add(FacilityData facilityData){
        facilityData.setCreateDate(new Date());
        entityManager.persist(facilityData);
        logger.info("FacilityDataDaoImpl添加facilityData成功！");
        return 1;
    }

    /**
     * 修改记录
     */
    @Override
    public Integer update(FacilityData facilityData){
        facilityData.setUpdateDate(new Date());
        entityManager.merge(facilityData);
        return 1;
    }

    /**
     * 删除记录
     */
    @Override
    public Integer delete(Long id){
        FacilityData facilityData = entityManager.find(FacilityData.class, id);
        entityManager.remove(facilityData);
        return 1;
    }

    /**
     * 删除记录 - 逻辑删除
     */
    @Override
    public Integer deleteLogic(FacilityData facilityData){
        facilityData.setDeleteStatus(true);
        return 1;
    }
}
